package com.changgou.pay;

import entity.IdWorker;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEurekaClient
public class PayApplication {
    public static void main(String[] args) {

        SpringApplication.run(PayApplication.class,args);
    }

    @Bean
    public IdWorker idWorker(){

        return  new IdWorker(0,2);
    }


    @Autowired
    private Environment env;
    /**
     *
     * mq:
     pay:
     exchange:
     order: exchange.order
     queue:
     order: queue.order
     routing:
     key: queue.order

     * @return
     */

    //创建交换机
    @Bean
    public DirectExchange createDirect() {
        return new DirectExchange(env.getProperty("mq.pay.exchange.order"));
    }

    //创建队列
    @Bean
    public Queue createqueue() {
        return new Queue(env.getProperty("mq.pay.queue.order"));
    }

    //创建 绑定
    @Bean
    public Binding createBinding() {
        return BindingBuilder.bind(createqueue()).to(createDirect()).with(env.getProperty("mq.pay.routing.key"));
    }


    //创建秒杀交换机
    @Bean
    public DirectExchange createSeckillDirect() {
        return new DirectExchange(env.getProperty("mq.pay.exchange.seckillorder"));
    }

    //创建秒杀队列
    @Bean
    public Queue createSeckillqueue() {
        return new Queue(env.getProperty("mq.pay.queue.seckillorder"));
    }

    //创建 秒杀的绑定
    @Bean
    public Binding createSeckillBinding() {
        return BindingBuilder.bind(createSeckillqueue()).to(createSeckillDirect()).with(env.getProperty("mq.pay.routing.seckillkey"));
    }
}
